#define _CRT_SECURE_NO_WARNINGS 1
#include<vector>
#include<queue>
using namespace std;

int bfs(vector<vector<int>>& v,  int a, int b);

vector<vector<int>> updateMatrix(vector<vector<int>>& mat)
{
    vector<vector<int>> ret(mat.size(), vector<int>(mat[0].size(), 0));
    int count = 0;
    for (int i = 0; i < mat.size(); i++)
    {
        for (int j = 0; j < mat[0].size(); j++)
        {
            if (mat[i][j] == 1)
            {
                count = bfs(mat, i, j);
                ret[i][j] = count;
            }
        }
    }
    return ret;
}

int bfs(vector<vector<int>>& v, int a, int b)
{
    int dx[4] = { 0,0,-1,1 };
    int dy[4] = { 1,-1,0,0 };
    queue<pair<int, int>> q;
    int count = 0;
    q.push({ a,b });
    while (q.size())
    {
        int sz = q.size();
        count++;

        for (int i = 0; i < sz; i++)
        {
            int m = q.front().first;
            int n = q.front().second;
            q.pop();
            for (int j = 0; j < 4; j++)
            {
                int x = m + dx[j];
                int y = n + dy[j];
                if (x >= 0 && y >= 0 && x < v.size() && y < v[0].size())
                {
                    if (v[x][y] == 0)
                    {
                        return count;
                    }
                    else
                    {
                        q.push({ x,y });
                    }
                }
            }
        }
    }
    return 0;
}


int main()
{
    vector<vector<int>> mat = { {0, 0, 0},{0, 1, 0},{1, 1, 1} };
    //vector<vector<int>> mat = { {0, 0, 0},{0, 1, 0},{0, 0, 0} };

    updateMatrix(mat);

    return 0;
}